package com.hsy.crm.web.service.asyn.marketprofit;

import com.hsy.crm.web.domain.entity.Message;
import com.hsy.crm.web.domain.entity.SaleCostDetail;
import com.hsy.crm.web.domain.entity.StoreProductDetail;
import com.hsy.crm.web.domain.po.account.DivResult;
import com.hsy.crm.web.mapper.SaleCostDetailMapper;
import com.hsy.crm.web.mapper.StoreProductDetailMapper;
import com.hsy.crm.web.utils.CalcUtils;
import com.hsy.crm.web.utils.DateUtils;
import com.hsy.crm.web.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 销售退货审核   入库
 */
@Service
public class SaleBackProfitService {

    @Autowired
    StoreProductDetailMapper storeProductDetailMapper;

    @Autowired
    SaleCostDetailMapper saleCostDetailMapper;

    @Autowired
    ProfitCommonService profitCommonService;

    @Autowired
    SaleProfitService saleProfitService;

    public void init(Message message, List<StoreProductDetail> storeProductDetails){
        for (StoreProductDetail storeProductDetail : storeProductDetails){
            this.init(message,storeProductDetail);
        }
    }

    /**
     * 1 指定销售单         取销售明细成本
     * 2 未指定销售单       取最后一次销售明细成本
     *
     * 原有销售单所有处理数据不变    销售退货 新入库
     *
     * 只查询该笔退货当初的售货成本 并在销售单成本明细上记录退了多少货 多少钱  只是记录
     * @param message
     * @param storeProductDetail
     */
    private void init(Message message,StoreProductDetail storeProductDetail){
        Long saleOrderDetailId = profitCommonService.getOrderDetailId(message,storeProductDetail);
        if( saleOrderDetailId > 0 ){
            StoreProductDetail saleStoreProductDetail = this.getSaleStoreProductDetail(message,saleOrderDetailId);
            this.updateOutPart(message,storeProductDetail,saleStoreProductDetail);
        }else{
            int result;
            do{
                StoreProductDetail saleStoreProductDetail = storeProductDetailMapper.getLastSaleStoreProductDetail(storeProductDetail);
                if( saleStoreProductDetail == null ){
                    break;
                }
                result = this.updateOutPart(message,storeProductDetail,saleStoreProductDetail);
            }while(result==0);
        }

        //after time re calc
        List<StoreProductDetail> storeProductDetails = storeProductDetailMapper.selectOutByTime(storeProductDetail);
        for( StoreProductDetail outStoreProductDetail : storeProductDetails ){
            profitCommonService.clearOutStoreProductDetail(message,outStoreProductDetail,Boolean.FALSE);
        }

        for( StoreProductDetail outStoreProductDetail : storeProductDetails ){
            saleProfitService.calcStoreProductDetailDone(message,outStoreProductDetail);
        }
    }

    /**
     * 在给定的销售明细上退货（退指定明细）
     * @param message
     * @param storeProductDetail    待退货的明细
     * @param saleStoreProductDetail    给定的销售明细
     * @return 0未处理完成 1已处理完成
     */
    private int updateOutPart(Message message,StoreProductDetail storeProductDetail,StoreProductDetail saleStoreProductDetail){
        long needBackNumber = storeProductDetail.getNumber();

        List<SaleCostDetail> saleCostDetails = profitCommonService.getOutSaleCostDetail(message,saleStoreProductDetail);
        for( SaleCostDetail saleCostDetail : saleCostDetails ){
            //剩余可退数量
            long elseNumber = saleCostDetail.getOutNumber() - saleCostDetail.getBackNumber();
            //剩余可退成本
            long elsePrice = saleCostDetail.getOutPrice() - saleCostDetail.getBackPrice();

            if( elseNumber < needBackNumber ){
                this.updateOut(message,elseNumber,elsePrice,storeProductDetail.getId(),saleCostDetail.getId());
                this.updateSaleCostDetail(message,saleCostDetail.getId(),elseNumber,elsePrice);
                needBackNumber -= elseNumber;
            }else if( elseNumber == needBackNumber ){
                this.updateOut(message,elseNumber,elsePrice,storeProductDetail.getId(),saleCostDetail.getId());
                this.updateSaleCostDetail(message,saleCostDetail.getId(),elseNumber,elsePrice);
                return 1;
            }else{
                DivResult result = CalcUtils.result(saleCostDetail.getOutNumber(),saleCostDetail.getOutPrice());

                Long number = needBackNumber;
                Long price = CalcUtils.mul(result.getSmall(),number);
                price = CalcUtils.div100(price);

                this.updateOut(message,number,price,storeProductDetail.getId(),saleCostDetail.getId());
                this.updateSaleCostDetail(message,saleCostDetail.getId(),number,price);
                return 1;
            }
        }

        return 0;
    }

    private void updateSaleCostDetail(Message message,Long saleCostDetailId,Long backNumber,Long backPrice){
        SaleCostDetail saleCostDetail = new SaleCostDetail();
        saleCostDetail.setId(saleCostDetailId);
        saleCostDetail.setCustomerId(message.getCustomerId());
        saleCostDetail.setBackPrice(backPrice);
        saleCostDetail.setBackNumber(backNumber);
        saleCostDetailMapper.updateOut(saleCostDetail);
    }

    private void updateOut(Message message,Long number,Long price,Long storeProductDetailId,Long saleCostDetailId){
        SaleCostDetail saleCostDetail = new SaleCostDetail();
        saleCostDetail.setId(UuidUtils.getUuid());
        saleCostDetail.setSaleCostDetailId(saleCostDetailId);
        saleCostDetail.setOutStoreProductDetailId(storeProductDetailId);
        saleCostDetail.setOutNumber(number);
        saleCostDetail.setOutPrice(price);
        saleCostDetail.setBackNumber(0L);
        saleCostDetail.setInStoreProductDetailId(0L);
        saleCostDetail.setBackPrice(0L);
        saleCostDetail.setCustomerId(message.getCustomerId());
        saleCostDetail.setOutTime(DateUtils.millis());
        saleCostDetail.setInTime(0l);
        saleCostDetailMapper.insert(saleCostDetail);


        StoreProductDetail updateOut = new StoreProductDetail();
        updateOut.setRelationStartSort(-1L);
        updateOut.setRelationEndSort(-1L);
        updateOut.setBackNumber(number);
        updateOut.setOutNumber(number);
        updateOut.setOutPrice(price);
        updateOut.setId(storeProductDetailId);
        updateOut.setCustomerId(message.getCustomerId());
        storeProductDetailMapper.addOut(updateOut);



    }

    private StoreProductDetail getSaleStoreProductDetail(Message message,Long saleOrderDetailId){
        StoreProductDetail detail = new StoreProductDetail();
        detail.setCustomerId(message.getCustomerId());
        detail.setOrderDetailId(saleOrderDetailId);
        return storeProductDetailMapper.selectBySaleOrderDetail(detail);
    }

}

